package de.ludo.util;

import java.util.List;

import javax.ejb.Stateless;

import de.ludo.dto.GameFieldTO;
import de.ludo.dto.GameTO;
import de.ludo.dto.PlayerTO;
import de.ludo.dto.UserTO;
import de.ludo.entities.Game;
import de.ludo.entities.Player;
import de.ludo.entities.User;

@Stateless
public class DtoAssembler {

	public UserTO createUserTO(User user) {
		UserTO dto = new UserTO();
		dto.setUserName(user.getUsername());
		dto.setPassword(user.getPassword());
		return dto;
	}

	public GameFieldTO createGameFieldTO(Game g) {
		GameFieldTO dto = new GameFieldTO();
		int[] result = new int[16];

		Player yellow = g.getPlayerYellow();
		Player green = g.getPlayerGreen();
		Player red = g.getPlayerRed();
		Player blue = g.getPlayerBlue();

		if (yellow != null) {
			result[0] = yellow.getCharacterOne();
			result[1] = yellow.getCharacterTwo();
			result[2] = yellow.getCharacterThree();
			result[3] = yellow.getCharacterFour();
		} else {
			result[0] = 0;
			result[1] = 0;
			result[2] = 0;
			result[3] = 0;
		}

		if (red != null) {
			result[4] = red.getCharacterOne();
			result[5] = red.getCharacterTwo();
			result[6] = red.getCharacterThree();
			result[7] = red.getCharacterFour();
		} else {
			result[4] = 0;
			result[5] = 0;
			result[6] = 0;
			result[7] = 0;
		}

		if (green != null) {
			result[8] = green.getCharacterOne();
			result[9] = green.getCharacterTwo();
			result[10] = green.getCharacterThree();
			result[11] = green.getCharacterFour();
		} else {
			result[8] = 0;
			result[9] = 0;
			result[10] = 0;
			result[11] = 0;
		}

		if (blue != null) {
			result[12] = blue.getCharacterOne();
			result[13] = blue.getCharacterTwo();
			result[14] = blue.getCharacterThree();
			result[15] = blue.getCharacterFour();
		} else {
			result[12] = 0;
			result[13] = 0;
			result[14] = 0;
			result[15] = 0;
		}

		dto.setGameField(result);

		return dto;
	}

	public GameTO createGameTO(Game game) {
		GameTO dto = new GameTO();
		dto.setGameId(game.getGameId());
		dto.setGameStatus(game.getStatus().name());
		// It can be that no other players are available
		if (game.getPlayerYellow() != null) {
			dto.setNameYellowPlayer(game.getPlayerYellow().getPlayerName());
		} else {
			dto.setNameYellowPlayer("");
		}
		if (game.getPlayerRed() != null) {
			dto.setNameRedPlayer(game.getPlayerRed().getPlayerName());
		} else {
			dto.setNameRedPlayer("");
		}
		if (game.getPlayerGreen() != null) {
			dto.setNameGreenPlayer(game.getPlayerGreen().getPlayerName());
		} else {
			dto.setNameGreenPlayer("");
		}
		if (game.getPlayerBlue() != null) {
			dto.setNameBluePlayer(game.getPlayerBlue().getPlayerName());
		} else {
			dto.setNameBluePlayer("");
		}
		return dto;
	}

	public PlayerTO createPlayerTO(Player player) {
		PlayerTO dto = new PlayerTO();
		dto.setSessionId(player.getSessionId());
		dto.setType(player.getType().toString());
		dto.setCharacterOne(player.getCharacterOne());
		dto.setCharacterTwo(player.getCharacterTwo());
		dto.setCharacterThree(player.getCharacterThree());
		dto.setCharacterFour(player.getCharacterFour());
		return dto;
	}

	/**
	 * Array of all available Games that can be entered by users
	 * 
	 * @param games
	 * @return
	 */
	public GameTO[] makeDTO(List<Game> games) {
		GameTO[] gamesTOList = new GameTO[games.size()];

		for (int i = 0; i < gamesTOList.length; i++) {
			Game g = games.get(i);
			gamesTOList[i] = createGameTO(g);
		}
		return gamesTOList;
	}

}
